package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.InvoicesControlDto;
import xyz.zhouzhaodong.crm.entity.InvoicesControl;
import xyz.zhouzhaodong.crm.entity.InvoicesControl;
import xyz.zhouzhaodong.crm.mapper.InvoicesControlMapper;
import xyz.zhouzhaodong.crm.mapper.InvoicesControlMapper;
import xyz.zhouzhaodong.crm.service.IInvoicesControlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 发票管理 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-07-03
 */
@Service
public class InvoicesControlServiceImpl extends ServiceImpl<InvoicesControlMapper, InvoicesControl> implements IInvoicesControlService {

    @Resource
    InvoicesControlMapper invoicesControlMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addInvoicesControl(InvoicesControl invoicesControl) {
        try {
            // 新增
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            invoicesControl.setId(id);
            invoicesControl.setCreateUser(invoicesControl.getStaffId());
            invoicesControl.setCreateTime(new Date());
            invoicesControlMapper.insert(invoicesControl);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteInvoicesControl(InvoicesControl invoicesControl) {
        try {
            InvoicesControl p = new InvoicesControl();
            p.setId(invoicesControl.getId());
            p.setFlag(1);
            p.setUpdateUser(invoicesControl.getStaffId());
            p.setUpdateTime(new Date());
            invoicesControlMapper.updateById(p);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateInvoicesControl(InvoicesControl invoicesControl) {
        try {
            invoicesControl.setUpdateUser(invoicesControl.getStaffId());
            invoicesControl.setUpdateTime(new Date());
            invoicesControl.setStaffId(null);
            invoicesControlMapper.updateById(invoicesControl);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectInvoicesControl(InvoicesControlDto invoicesControlDto) {
        Page<InvoicesControlDto> page = new Page<>(invoicesControlDto.getPageNo(),
                invoicesControlDto.getPageSize() == 0 ? 10 : invoicesControlDto.getPageSize());
        if(invoicesControlDto.getFlag() == null){
            invoicesControlDto.setFlag(0);
        }
        IPage<InvoicesControlDto> list = invoicesControlMapper.selectInvoicesControl(invoicesControlDto, page);
        return RestMessage.success(list);
    }


}
